/*
 * Copyright (c) 2012 Johann Hanne
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include <stdio.h>

#include <putchar.h> /* Must be provided externally (i.e. not part of clib) */

/* We do not currently implement a file layer, so these are all stubs,
   except when stdout/stderr is used */

FILE _stdio_files[] = {
    { 0 },
    { 0 },
    { 0 }
};

FILE *stdin  = &_stdio_files[0];
FILE *stdout = &_stdio_files[1];
FILE *stderr = &_stdio_files[2];

FILE *fopen(const char *path, const char *mode) {
    return NULL;
}

FILE *freopen(const char *path, const char *mode, FILE *stream) {
    return NULL;
}

size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream) {
    return 0;
}

int fclose(FILE *fp) {
    return 0;
}

size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) {
    if (stream == stdout || stream == stderr) {
        size_t size_c, nmemb_c;
        char *p = (char *)ptr;

        for (nmemb_c = 0; nmemb_c < nmemb; nmemb_c++) {
            for (size_c = 0; size_c < size; size_c++) {
                putchar(*p);
                p++;
            }
        }
    }

    return 0;
}

int fgetc(FILE *stream) {
    return 0;
}

char *fgets(char *s, int size, FILE *stream) {
    return NULL;
}

int getc(FILE *stream) {
    return EOF;
}

int getchar(void) {
    return EOF;
}

char *gets(char *s) {
    return NULL;
}

int ungetc(int c, FILE *stream) {
    return EOF;
}

void clearerr(FILE *stream) {
}

int feof(FILE *stream) {
    return 1;
}

int ferror(FILE *stream) {
    return 1;
}

int fileno(FILE *stream) {
    return -1;
}

int fflush(FILE *stream) {
    return 0;
}
